package com.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shop.mapper.GoodsMapper;
import com.shop.pojo.Goods;
import com.shop.service.GoodsService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    private GoodsMapper goodsMapper;

    @Override
    public void add(Goods goods) {
        goodsMapper.insert(goods);
    }

    @Override
    public Page<Goods> pageQuery(Integer pagenum, Integer pagesize, String pageQuery) {
        QueryWrapper<Goods> wrapper=new QueryWrapper<>();
        Page<Goods> page=new Page<>(pagenum,pagesize);
        if (pageQuery!=null&&pageQuery.length()>0){
            wrapper.like("goods_id",pageQuery).or().like("goods_name",pageQuery);
            page=goodsMapper.selectPage(page,wrapper);
            List<Goods> goodsList = page.getRecords();
            for (Goods good :
                    goodsList) {
                String kindsName = goodsMapper.findKindsNameByGoodsId(good.getId());
                good.setKindsName(kindsName);
            }
            page.setRecords(goodsList);
        }else {
            page=goodsMapper.selectPage(page,null);
            List<Goods> goodsList = page.getRecords();
            for (Goods good :
                    goodsList) {
                String kindsName = goodsMapper.findKindsNameByGoodsId(good.getId());
                good.setKindsName(kindsName);
            }
            page.setRecords(goodsList);
        }
        return page;
    }

    @Override
    public void deleteById(Integer id) {
        long count=goodsMapper.findCountByGoodsId(id);
        if(count>0){
            new RuntimeException();
        }
        goodsMapper.deleteById(id);
    }

    @Override
    public void edit(Goods Goods) {
        goodsMapper.updateById(Goods);
    }

    @Override
    public Goods findById(Integer id) {
        return goodsMapper.selectById(id);
    }

    @Override
    public List<Goods> findAll() {
        return goodsMapper.selectList(null);
    }

    @Override
    public Goods findGoodsName(String goodsname) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("goods_name",goodsname);
        return goodsMapper.selectOne(queryWrapper);
    }

    @Override
    public Page<Goods> goodsPage(Integer gid, Integer pagenum, Integer pagesize) {
        Page<Goods> page = new Page<>(pagenum,pagesize);
        QueryWrapper<Goods> wrapper = new QueryWrapper<>();
        if (gid==0){
            page = goodsMapper.selectPage(page,null);
        }else{
            wrapper.eq("goods_kind",gid);
            page = goodsMapper.selectPage(page,wrapper);
        }
        return page;
    }

    @Override
    public Goods getGoods(int id) {
        QueryWrapper<Goods> wrapper = new QueryWrapper<>();
        wrapper.eq("goods_id",id);
        Goods goods = goodsMapper.selectOne(wrapper);
        return goods;
    }
}
